package com.example.utils;


import com.baomidou.mybatisplus.extension.api.IErrorCode;
import com.baomidou.mybatisplus.extension.enums.ApiErrorCode;
import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Optional;

/**
 * REST API 返回结果
 *
 * @author hubin
 * @since 2018-06-05
 */
// 使用度较低，如果使用请及时迁移本地 3.5.0 移除

@Data
public class RestResult<T> implements Serializable {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    /**
     * 业务错误码
     */
    private long code;
    /**
     * 结果集
     */
    private T data;
    /**
     * 描述
     */
    private String msg;

    public RestResult() {
        // to do nothing
    }


    public static <T> RestResult<T> ok(T data) {
        RestResult<T> apiResult = new RestResult<T>();

        apiResult.setData(data);
        return apiResult;
    }

    public static <T> RestResult<T> failed(String msg) {
        RestResult<T> apiResult = new RestResult<>();

        apiResult.setMsg(msg);
        return apiResult;
    }

    public static <T> RestResult<T> failed(int errorCode) {
        RestResult<T> apiResult = new RestResult<>();
        apiResult.setCode(errorCode);
        return apiResult;
    }

    public static <T> RestResult<T> success(T data, int code,String msg) {

        RestResult<T> apiResult = new RestResult<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    private static <T> RestResult<T> success(T data, long code, String msg) {
        RestResult<T> apiResult = new RestResult<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

}